/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ResultListPanel.java
 *
 * Created on 29.12.2008, 21:27:13
 */
package trim.gui.testing.components;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Arrays;
import javax.swing.JFileChooser;
import javax.swing.JTabbedPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import trim.common.inputdata.InputDataException;
import trim.gui.testing.AppState;
import trim.gui.testing.PlaginManager;
import trim.gui.testing.export.ExportManager;
import trim.gui.testing.models.ResultListTableModel;
import trim.gui.testing.objects.ResultsListItem;

/**
 *
 * @author Дмитрий
 */
public class ResultListPanel extends javax.swing.JPanel
{

  ResultListTableModel resultListTableModel;
  JTabbedPane resultTabbedPane;

  /** Creates new form ResultListPanel */
  public ResultListPanel(ResultListTableModel resultListTableModel,JTabbedPane resultTabbedPane)
  {
    this.resultListTableModel=resultListTableModel;
    this.resultTabbedPane=resultTabbedPane;
    initComponents();
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    popupMenuResultList = new javax.swing.JPopupMenu();
    miShowHide = new javax.swing.JMenuItem();
    popupMenuTests = new javax.swing.JPopupMenu();
    miClear = new javax.swing.JMenuItem();
    miRemove = new javax.swing.JMenuItem();
    jToolBar1 = new javax.swing.JToolBar();
    jLabel1 = new javax.swing.JLabel();
    btExportXLS = new javax.swing.JButton();
    jSplitPane1 = new javax.swing.JSplitPane();
    spRequests = new javax.swing.JScrollPane();
    tRequests = new javax.swing.JTable();
    spResults = new javax.swing.JScrollPane();
    tResults = new javax.swing.JTable();

    miShowHide.setText("Show/Hide");
    miShowHide.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        miShowHideActionPerformed(evt);
      }
    });
    popupMenuResultList.add(miShowHide);

    miClear.setText("Clear");
    miClear.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        miClearActionPerformed(evt);
      }
    });
    popupMenuTests.add(miClear);

    miRemove.setText("Remove");
    miRemove.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        miRemoveActionPerformed(evt);
      }
    });
    popupMenuTests.add(miRemove);

    jToolBar1.setRollover(true);

    jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
    jLabel1.setText("Export to:");
    jToolBar1.add(jLabel1);

    btExportXLS.setFont(new java.awt.Font("Tahoma", 1, 11));
    btExportXLS.setText("XLS");
    btExportXLS.setFocusable(false);
    btExportXLS.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    btExportXLS.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    btExportXLS.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btExportXLSActionPerformed(evt);
      }
    });
    jToolBar1.add(btExportXLS);

    jSplitPane1.setResizeWeight(0.3);

    spRequests.setPreferredSize(new java.awt.Dimension(400, 275));

    tRequests.setAutoCreateRowSorter(true);
    tRequests.setModel(resultListTableModel);
    tRequests.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mouseClicked(java.awt.event.MouseEvent evt) {
        tRequestsMouseClicked(evt);
      }
      public void mousePressed(java.awt.event.MouseEvent evt) {
        tRequestsMousePressed(evt);
      }
      public void mouseReleased(java.awt.event.MouseEvent evt) {
        tRequestsMouseReleased(evt);
      }
    });
    tRequests.addInputMethodListener(new java.awt.event.InputMethodListener() {
      public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
        tRequestsCaretPositionChanged(evt);
      }
      public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
      }
    });
    tRequests.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
      public void propertyChange(java.beans.PropertyChangeEvent evt) {
        tRequestsPropertyChange(evt);
      }
    });
    tRequests.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyPressed(java.awt.event.KeyEvent evt) {
        tRequestsKeyPressed(evt);
      }
      public void keyTyped(java.awt.event.KeyEvent evt) {
        tRequestsKeyTyped(evt);
      }
    });
    spRequests.setViewportView(tRequests);

    jSplitPane1.setLeftComponent(spRequests);

    spResults.setPreferredSize(new java.awt.Dimension(700, 275));

    tResults.setAutoCreateRowSorter(true);
    tResults.setModel(resultListTableModel.getResultsTableModel());
    tResults.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mouseClicked(java.awt.event.MouseEvent evt) {
        tResultsMouseClicked(evt);
      }
      public void mousePressed(java.awt.event.MouseEvent evt) {
        tResultsMousePressed(evt);
      }
      public void mouseReleased(java.awt.event.MouseEvent evt) {
        tResultsMouseReleased(evt);
      }
    });
    spResults.setViewportView(tResults);

    jSplitPane1.setRightComponent(spResults);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 788, Short.MAX_VALUE)
      .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 788, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addGap(0, 0, 0)
        .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 283, Short.MAX_VALUE))
    );
  }// </editor-fold>//GEN-END:initComponents

  private void miClearActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_miClearActionPerformed
  {//GEN-HEADEREND:event_miClearActionPerformed
    resultListTableModel.clear();
}//GEN-LAST:event_miClearActionPerformed

  private void miRemoveActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_miRemoveActionPerformed
  {//GEN-HEADEREND:event_miRemoveActionPerformed
    int start=tRequests.getSelectedRow();
    if(start<0)
      return;
    int selCount=tRequests.getSelectedRowCount();
    int[] a=new int[selCount];
    for(int i=0;i<selCount;i++)
    {
      a[i]=tRequests.convertRowIndexToModel(i+start);
    }
    Arrays.sort(a);
    for(int i=a.length-1;i>=0;i--)
    {
      resultListTableModel.removeTest(a[i]);
    }
  }//GEN-LAST:event_miRemoveActionPerformed

  private void miShowHideActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_miShowHideActionPerformed
  {//GEN-HEADEREND:event_miShowHideActionPerformed
    int start=tResults.getSelectedRow();
    if(start<0)
      return;
    int selCount=tResults.getSelectedRowCount();
    for(int i=start;i<selCount+start;i++)
    {
      int r=tResults.convertRowIndexToModel(i);
      ResultsListItem rli=resultListTableModel.getResultsTableModel().getResult(r);
      if(rli.getResultTabPanel()==null)
      {
        rli.setResultTabPanel(new ResultTabPanel(rli));
        resultTabbedPane.add(rli.getMethodName(),rli.getResultTabPanel());
        resultTabbedPane.setSelectedComponent(rli.getResultTabPanel());
        resultListTableModel.getResultsTableModel().fireTableDataChanged();
      }else
      {
        resultTabbedPane.setSelectedComponent(rli.getResultTabPanel());
        //resultListTableModel.getResultsTableModel().hideResult(r);
      }
    }
  }//GEN-LAST:event_miShowHideActionPerformed

  private void tResultsMouseReleased(java.awt.event.MouseEvent evt)//GEN-FIRST:event_tResultsMouseReleased
  {//GEN-HEADEREND:event_tResultsMouseReleased
    if(evt.isPopupTrigger())
      popupMenuResultList.show(evt.getComponent(),evt.getX(),evt.getY());
  }//GEN-LAST:event_tResultsMouseReleased

  private void tRequestsMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_tRequestsMouseClicked
  {//GEN-HEADEREND:event_tRequestsMouseClicked
  }//GEN-LAST:event_tRequestsMouseClicked

  private void tRequestsMouseReleased(java.awt.event.MouseEvent evt)//GEN-FIRST:event_tRequestsMouseReleased
  {//GEN-HEADEREND:event_tRequestsMouseReleased
    if(evt.isPopupTrigger())
      popupMenuTests.show(evt.getComponent(),evt.getX(),evt.getY());
    else
    {
      int r=tRequests.getSelectedRow();
      if(r<0)
        return;
      r=tRequests.convertRowIndexToModel(r);
      resultListTableModel.selectTest(r);
    }
  }//GEN-LAST:event_tRequestsMouseReleased

  private void tResultsMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_tResultsMouseClicked
  {//GEN-HEADEREND:event_tResultsMouseClicked
    if(evt.getClickCount()<2)
      return;
    miShowHideActionPerformed(null);
  }//GEN-LAST:event_tResultsMouseClicked

  private void btExportXLSActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btExportXLSActionPerformed
  {//GEN-HEADEREND:event_btExportXLSActionPerformed
    JFileChooser fileChooser=new JFileChooser(AppState.CURRENT_FILE);
    FileFilter ff=new FileNameExtensionFilter("*.xls","xls");
    fileChooser.setFileFilter(ff);
    int r=fileChooser.showSaveDialog(this);
    if(r==JFileChooser.APPROVE_OPTION)
    {
      try
      {        
        File f=fileChooser.getSelectedFile();
        if(!f.getName().toLowerCase().endsWith(".xls"))
          f=new File(f.getAbsolutePath()+".xls");
        AppState.CURRENT_FILE=f.getParentFile();
        FileOutputStream fos=new FileOutputStream(f);
        ExportManager.exportResultListToXLS(fos,resultListTableModel.getResults(),
            AppState.getPlaginManager().getUsedMethods());
        fos.close();
        //mainInputData.saveDataToXML(fileChooser.getSelectedFile());
      }catch(Exception ex)
      {
        ex.printStackTrace();
      }
    }
  }//GEN-LAST:event_btExportXLSActionPerformed

  private void tRequestsCaretPositionChanged(java.awt.event.InputMethodEvent evt)//GEN-FIRST:event_tRequestsCaretPositionChanged
  {//GEN-HEADEREND:event_tRequestsCaretPositionChanged
    System.out.println("caret change");
  }//GEN-LAST:event_tRequestsCaretPositionChanged

  private void tRequestsPropertyChange(java.beans.PropertyChangeEvent evt)//GEN-FIRST:event_tRequestsPropertyChange
  {//GEN-HEADEREND:event_tRequestsPropertyChange
    System.out.println("property change");
  }//GEN-LAST:event_tRequestsPropertyChange

  private void tRequestsKeyTyped(java.awt.event.KeyEvent evt)//GEN-FIRST:event_tRequestsKeyTyped
  {//GEN-HEADEREND:event_tRequestsKeyTyped
    System.out.println("Key typed change");
  }//GEN-LAST:event_tRequestsKeyTyped

  private void tRequestsKeyPressed(java.awt.event.KeyEvent evt)//GEN-FIRST:event_tRequestsKeyPressed
  {//GEN-HEADEREND:event_tRequestsKeyPressed
   int r=tRequests.getSelectedRow();
      if(r<0)
        return;
      r=tRequests.convertRowIndexToModel(r);
      resultListTableModel.selectTest(r);
  }//GEN-LAST:event_tRequestsKeyPressed

	private void tRequestsMousePressed(java.awt.event.MouseEvent evt)//GEN-FIRST:event_tRequestsMousePressed
	{//GEN-HEADEREND:event_tRequestsMousePressed
if(evt.isPopupTrigger())
      popupMenuTests.show(evt.getComponent(),evt.getX(),evt.getY());
	}//GEN-LAST:event_tRequestsMousePressed

	private void tResultsMousePressed(java.awt.event.MouseEvent evt)//GEN-FIRST:event_tResultsMousePressed
	{//GEN-HEADEREND:event_tResultsMousePressed
    if(evt.isPopupTrigger())
      popupMenuResultList.show(evt.getComponent(),evt.getX(),evt.getY());
	}//GEN-LAST:event_tResultsMousePressed

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton btExportXLS;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JSplitPane jSplitPane1;
  private javax.swing.JToolBar jToolBar1;
  private javax.swing.JMenuItem miClear;
  private javax.swing.JMenuItem miRemove;
  private javax.swing.JMenuItem miShowHide;
  private javax.swing.JPopupMenu popupMenuResultList;
  private javax.swing.JPopupMenu popupMenuTests;
  private javax.swing.JScrollPane spRequests;
  private javax.swing.JScrollPane spResults;
  private javax.swing.JTable tRequests;
  private javax.swing.JTable tResults;
  // End of variables declaration//GEN-END:variables
}
